using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NewMainMenu_Slots : MonoBehaviour
{
	[Serializable]
	public class SlotSymbol
	{
		public Sprite sprite;

		public float odds;

		public float triplepayout;

		public AudioClip selectsound;

		public AudioClip triplesound;
	}

	[Header("-= Slot Settings =-")]
	public RectTransform[] columns;

	public int visibleRows = 3;

	public float spinDuration = 2f;

	public float spinSpeed = 500f;

	public float alignLerpSpeed = 5f;

	[SerializeField]
	private TextMeshProUGUI TextMoney;

	[SerializeField]
	private TextMeshProUGUI ControlsText;

	[Header("-= Slot Sound =-")]
	[SerializeField]
	private AudioClip SFX_SlotClick;

	[SerializeField]
	private AudioClip SFX_SpendMoney;

	[SerializeField]
	private AudioClip Mus_Gambling;

	[Header("-= Symbols =-")]
	public List<SlotSymbol> slotSymbols = new List<SlotSymbol>();

	private List<Image[]> columnImages = new List<Image[]>();

	private float slotHeight;

	private int symbolsPerColumn;

	private bool currentlySpinning;

	[SerializeField]
	private GameObject ConsoleMenu;

	private bool AllTextShown;

	private bool wasEnabled;

	public List<TextMeshProUGUI> BackgroundTextLines = new List<TextMeshProUGUI>();

	[SerializeField]
	private List<int> BackgroundTextMaxCount = new List<int>();

	private int spinCounter;

	private void Start()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Expected O, but got Unknown
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		symbolsPerColumn = visibleRows + 2;
		Rect rect = columns[0].rect;
		slotHeight = ((Rect)(ref rect)).height / (float)visibleRows;
		RectTransform[] array = columns;
		foreach (RectTransform val in array)
		{
			Image[] array2 = (Image[])(object)new Image[symbolsPerColumn];
			for (int j = 0; j < symbolsPerColumn; j++)
			{
				GameObject val2 = new GameObject($"Slot_{j}", new Type[1] { typeof(Image) });
				val2.transform.SetParent((Transform)(object)val, false);
				array2[j] = val2.GetComponent<Image>();
				RectTransform rectTransform = ((Graphic)array2[j]).rectTransform;
				rect = val.rect;
				rectTransform.sizeDelta = new Vector2(((Rect)(ref rect)).width - 20f, slotHeight);
				float num = slotHeight * (float)(symbolsPerColumn - visibleRows) / 2f;
				((Graphic)array2[j]).rectTransform.anchoredPosition = new Vector2(0f, num - (float)j * slotHeight);
			}
			columnImages.Add(array2);
		}
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			BackgroundTextMaxCount.Add(((TMP_Text)backgroundTextLine).text.Length);
			((TMP_Text)backgroundTextLine).maxVisibleCharacters = 0;
		}
		ResetColumns();
	}

	private void OnDisable()
	{
		wasEnabled = false;
		AllTextShown = false;
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			((TMP_Text)backgroundTextLine).maxVisibleCharacters = 0;
		}
		currentlySpinning = false;
		((MonoBehaviour)this).StopCoroutine("SpinRoutine");
	}

	private void Update()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && !currentlySpinning)
		{
			if (SecurePlayerPrefs.GetSecureInt("TotalCash") >= 10)
			{
				Spin();
				NewMainMenuManager.instance.MenuSource.PlayOneShot(SFX_SpendMoney);
				SecurePlayerPrefs.SetSecureInt("TotalCash", SecurePlayerPrefs.GetSecureInt("TotalCash") - 10);
				((TMP_Text)TextMoney).text = string.Format("{0}$", SecurePlayerPrefs.GetSecureInt("TotalCash"));
			}
			else
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			}
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && !currentlySpinning)
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			ConsoleMenu.SetActive(true);
			((Component)this).gameObject.SetActive(false);
			MusicManager.StopSong(Fade: false, 0f);
			CameraManager.instance.ReverbFilter.reverbPreset = (AudioReverbPreset)1;
		}
		if (!wasEnabled && ((Component)this).gameObject.activeSelf)
		{
			wasEnabled = true;
			((TMP_Text)TextMoney).text = string.Format("{0}$", SecurePlayerPrefs.GetSecureInt("TotalCash"));
			((MonoBehaviour)this).StartCoroutine(ShowBackgroundText());
			CameraManager.instance.ReverbFilter.reverbPreset = (AudioReverbPreset)0;
			MusicManager.PlaySong(Mus_Gambling, FadePreviousSong: false, 0f);
			((TMP_Text)ControlsText).text = $"按 {PlayerInput.Instance.Key_Confirm} 摇奖\n按 {PlayerInput.Instance.Key_Cancel} 返回";
		}
	}

	public void ResetColumns()
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		foreach (Image[] columnImage in columnImages)
		{
			for (int i = 0; i < columnImage.Length; i++)
			{
				columnImage[i].sprite = GetRandomSymbol();
				((Graphic)columnImage[i]).rectTransform.anchoredPosition = new Vector2(0f, slotHeight * (float)(symbolsPerColumn - visibleRows) / 2f - (float)i * slotHeight);
			}
		}
	}

	public void Spin()
	{
		currentlySpinning = true;
		spinCounter++;
		if (spinCounter % 10 == 0)
		{
			ResetColumns();
		}
		((MonoBehaviour)this).StartCoroutine(SpinRoutine());
		DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.CheekyGrin);
	}

	private IEnumerator SpinRoutine()
	{
		float elapsedTime = 0f;
		while (elapsedTime < spinDuration)
		{
			float num = Mathf.Clamp01(elapsedTime / (spinDuration / 4f));
			foreach (Image[] columnImage in columnImages)
			{
				for (int i = 0; i < columnImage.Length; i++)
				{
					RectTransform rectTransform = ((Graphic)columnImage[i]).rectTransform;
					rectTransform.anchoredPosition += new Vector2(0f, (0f - spinSpeed) * num * Time.deltaTime);
					if (rectTransform.anchoredPosition.y <= (0f - slotHeight) * (float)(symbolsPerColumn - 1))
					{
						rectTransform.anchoredPosition += new Vector2(0f, slotHeight * (float)symbolsPerColumn + slotHeight);
						columnImage[i].sprite = GetRandomSymbol();
						CutsceneUtils.PlaySound(SFX_SlotClick, CutsceneUtils.DRH_MixerChannels.Effect, 0.5f, Random.Range(0.9f, 1.1f));
					}
				}
			}
			elapsedTime += Time.deltaTime;
			yield return null;
		}
		AlignColumns();
	}

	private Sprite GetRandomSymbol()
	{
		float num = 0f;
		foreach (SlotSymbol slotSymbol in slotSymbols)
		{
			num += slotSymbol.odds;
		}
		float num2 = Random.Range(0f, num);
		float num3 = 0f;
		foreach (SlotSymbol slotSymbol2 in slotSymbols)
		{
			num3 += slotSymbol2.odds;
			if (num2 <= num3)
			{
				return slotSymbol2.sprite;
			}
		}
		return slotSymbols[0].sprite;
	}

	private void AlignColumns()
	{
		((MonoBehaviour)this).StartCoroutine(AlignRoutine());
	}

	private IEnumerator AlignRoutine()
	{
		bool aligning = true;
		while (aligning)
		{
			aligning = false;
			for (int columnIndex = 0; columnIndex < columnImages.Count; columnIndex++)
			{
				Image[] array = columnImages[columnIndex];
				for (int i = 0; i < array.Length; i++)
				{
					RectTransform rectTransform = ((Graphic)array[i]).rectTransform;
					float num = Mathf.Round(rectTransform.anchoredPosition.y / slotHeight) * slotHeight;
					float num2 = Mathf.Lerp(rectTransform.anchoredPosition.y, num, Time.deltaTime * alignLerpSpeed);
					rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, num2);
					if (Mathf.Abs(rectTransform.anchoredPosition.y - num) > 0.1f)
					{
						aligning = true;
					}
				}
				yield return null;
			}
		}
		bool playWinSound = false;
		AudioClip winSound = null;
		for (int columnIndex = 0; columnIndex < columnImages.Count; columnIndex++)
		{
			Image[] array2 = columnImages[columnIndex];
			int num3 = visibleRows / 2;
			Image val = array2[num3];
			SlotSymbol symbolFromImage = GetSymbolFromImage(val.sprite);
			if (symbolFromImage != null)
			{
				MonoBehaviour.print((object)$"Column {columnIndex + 1}: {((Object)symbolFromImage.sprite).name}");
				CutsceneUtils.PlaySound(symbolFromImage.selectsound, CutsceneUtils.DRH_MixerChannels.Effect, 0.8f);
				if ((Object)(object)columnImages[0][visibleRows / 2].sprite == (Object)(object)columnImages[1][visibleRows / 2].sprite && (Object)(object)columnImages[1][visibleRows / 2].sprite == (Object)(object)columnImages[2][visibleRows / 2].sprite)
				{
					Sprite sprite = columnImages[0][visibleRows / 2].sprite;
					SlotSymbol symbolFromImage2 = GetSymbolFromImage(sprite);
					if (symbolFromImage2 != null)
					{
						playWinSound = true;
						winSound = symbolFromImage2.triplesound;
						float triplepayout = symbolFromImage2.triplepayout;
						SecurePlayerPrefs.SetSecureInt("TotalCash", SecurePlayerPrefs.GetSecureInt("TotalCash") + Mathf.RoundToInt(triplepayout), disableTotalCashDisplay: true);
						((TMP_Text)TextMoney).text = string.Format("{0}$", SecurePlayerPrefs.GetSecureInt("TotalCash"));
						DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.Shock_NoSound);
						MonoBehaviour.print((object)$"共获得奖金：{triplepayout}$");
					}
				}
				else
				{
					DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.Annoyed);
				}
			}
			yield return (object)new WaitForSeconds(0.2f);
		}
		if (playWinSound && (Object)(object)winSound != (Object)null)
		{
			MusicManager.PauseMusic();
			CutsceneUtils.PlaySound(winSound);
			yield return (object)new WaitForSeconds(winSound.length);
			DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.Crying);
			MusicManager.ResumeMusic();
		}
		currentlySpinning = false;
	}

	private IEnumerator ShowBackgroundText()
	{
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX"))
		{
			List<TextMeshProUGUI> finishedTexts = new List<TextMeshProUGUI>();
			while (!AllTextShown)
			{
				yield return null;
				foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
				{
					if (!finishedTexts.Contains(backgroundTextLine) && ((TMP_Text)backgroundTextLine).maxVisibleCharacters >= ((TMP_Text)backgroundTextLine).text.Length)
					{
						finishedTexts.Add(backgroundTextLine);
					}
					else if (((TMP_Text)backgroundTextLine).maxVisibleCharacters < ((TMP_Text)backgroundTextLine).text.Length)
					{
						float num = 100 + Random.Range(-60, -10);
						((TMP_Text)backgroundTextLine).maxVisibleCharacters = ((TMP_Text)backgroundTextLine).maxVisibleCharacters + Mathf.CeilToInt(num * Time.deltaTime);
						((TMP_Text)backgroundTextLine).maxVisibleCharacters = Mathf.Clamp(((TMP_Text)backgroundTextLine).maxVisibleCharacters, 0, ((TMP_Text)backgroundTextLine).text.Length);
						NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
					}
				}
				if (CompareTextLists(finishedTexts, BackgroundTextLines))
				{
					AllTextShown = true;
					MonoBehaviour.print((object)"all text shown");
				}
			}
			yield break;
		}
		foreach (TextMeshProUGUI backgroundTextLine2 in BackgroundTextLines)
		{
			((TMP_Text)backgroundTextLine2).maxVisibleCharacters = ((TMP_Text)backgroundTextLine2).text.Length;
		}
	}

	private bool CompareTextLists(List<TextMeshProUGUI> list1, List<TextMeshProUGUI> list2)
	{
		if (list1.Count != list2.Count)
		{
			return false;
		}
		for (int i = 0; i < list1.Count; i++)
		{
			if (((TMP_Text)list1[i]).text != ((TMP_Text)list2[i]).text)
			{
				return false;
			}
		}
		return true;
	}

	private SlotSymbol GetSymbolFromImage(Sprite sprite)
	{
		foreach (SlotSymbol slotSymbol in slotSymbols)
		{
			if ((Object)(object)slotSymbol.sprite == (Object)(object)sprite)
			{
				return slotSymbol;
			}
		}
		return null;
	}
}
